package org.zwd.computePrice.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.zwd.computePrice.domain.Crab;
import org.zwd.computePrice.domain.CrabPrice;
import org.zwd.computePrice.domain.Order;
import org.zwd.computePrice.domain.Total;
import org.zwd.computePrice.service.CrabService;
import org.zwd.computePrice.service.OrderService;
import org.zwd.computePrice.util.DateUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OrderAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private String name;
	private int userid;
	private int malecrabids[];
	private int femalecrabids[];
	private double malemounts[];
	private double femalemounts[];
	private double malecrabPrice[];
	private double femalecrabPrice[];
	private String maledescribes[];
	private String femaledescribes[];
	
	private int realPay;
	private String time;
	private Date date;
	private String id;
	private int radio;
	private int isOrder;
	
	private CrabService crabService = new CrabService();
	private OrderService orderService = new OrderService();
	
	public String create() throws Exception{
		List<Crab> crabList = new ArrayList<Crab>();
		if(malecrabids !=null && malecrabids.length>0){
			for(int i = 0; i<malecrabids.length;i++){
				if(malemounts[i] != 0.0){
					Crab crab = crabService.findCrabById(malecrabids[i]);
					CrabPrice crabPrice = new CrabPrice();
					crabPrice.setPrice(malecrabPrice[i]);
					crab.setCrabPrice(crabPrice);
					crab.setNote(maledescribes[i]);
					crab.setNumber(malemounts[i]);
					crabList.add(crab);
				}
			}
		}
		
		if(femalecrabids !=null && femalecrabids.length>0){
			for(int i = 0; i<femalecrabids.length;i++){
				if(femalemounts[i] != 0.0){
					Crab crab = crabService.findCrabById(femalecrabids[i]);
					CrabPrice crabPrice = new CrabPrice();
					crabPrice.setPrice(femalecrabPrice[i]);
					crab.setCrabPrice(crabPrice);
					crab.setNote(femaledescribes[i]);
					crab.setNumber(femalemounts[i]);
					crabList.add(crab);
				}
			}
		}
		
		String orderid = orderService.createOrder(crabList,name,userid,isOrder);
		
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("orderid", orderid);
		request.setAttribute("isOrder", isOrder);
		
		return "create";
	}
	
	public String result() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		String orderid = (String) request.getAttribute("orderid");
		int  isOrder = (int) request.getAttribute("isOrder");
		Order order = orderService.searchOrderByOrderId(orderid,isOrder);
		ActionContext.getContext().put("order", order);
		ActionContext.getContext().put("isOrder", isOrder);
		//System.out.println(order);
		return "result";
	}
	
	public String search() throws Exception{
		ActionContext.getContext().put("radio", 1);
		ActionContext.getContext().put("isOrder", isOrder);
		return "search";
	}
	
	public String searchNumberByDate() throws Exception{
		Total orderTotal=null;
		Total saleTotal =null;
		List<Order> orderList = null;
		List<Order> saleList = null;
		Date date = new Date();
		orderList = orderService.searchOrderToday(1);
		saleList = orderService.searchOrderToday(0);
		if(orderList != null && orderList.size() > 0){
			orderTotal = orderService.getTotal(orderList);
			ActionContext.getContext().put("orderTotal", orderTotal);
		}
		if (saleList != null && saleList.size() > 0) {
			saleTotal = orderService.getTotal(saleList);
			ActionContext.getContext().put("saleTotal", saleTotal);
		}
		if(orderList == null && saleList==null){
			String msg = "对不起，还无今日的规格汇总！";
			ActionContext.getContext().put("msg", msg);
		}
		ActionContext.getContext().put("datestr", DateUtils.date2String(date));
		return "searchNumberByDate";
	}
	
	public String searchNumberByTimeUI() throws Exception{
		return "searchNumberByTimeUI";
	}
	
	
	public String searchNumberByTime() throws Exception{
		Total orderTotal=null;
		Total saleTotal =null;
		List<Order> orderList = null;
		List<Order> saleList = null;
		
		if(time != null){
			String[] splitTime = time.split("~");
			if(splitTime != null && splitTime.length >1){
				String startTime = splitTime[0];
				String endTime = splitTime[1];
				orderList = orderService.searchOrderByTime(startTime, endTime,1);
				saleList = orderService.searchOrderByTime(startTime, endTime,0);
			}
		}
		if(orderList != null && orderList.size() > 0){
			orderTotal = orderService.getTotal(orderList);
			ActionContext.getContext().put("orderTotal", orderTotal);
		}
		if (saleList != null && saleList.size() > 0) {
			saleTotal = orderService.getTotal(saleList);
			ActionContext.getContext().put("saleTotal", saleTotal);
		}
		if(orderList == null && saleList==null){
			String msg = "对不起，无规格汇总，请核对信息！";
			ActionContext.getContext().put("msg", msg);
		}
		ActionContext.getContext().put("timestr", time);
		return "searchNumberByTime";
	}
	
	
	public String searchByDate() throws Exception{
		List<Order> orderList = null;
		Date date = new Date();
		orderList = orderService.searchOrderToday(isOrder);
		if(orderList != null && orderList.size() > 0){
			ActionContext.getContext().put("dateOrderList", orderList);
			ActionContext.getContext().put("datestr", DateUtils.date2String(date));
			Total total = new Total(orderList);
			ActionContext.getContext().put("total", total);
			ActionContext.getContext().put("isOrder", isOrder);
		}else{
			String msg = "对不起，还无今日的清单！";
			ActionContext.getContext().put("msg", msg);
		}
		return "searchByDate";
	}
	
	public String searchByTime() throws Exception{
		//System.out.println(time);
		List<Order> orderList = null;
		if(time != null){
			String[] splitTime = time.split("~");
			if(splitTime != null && splitTime.length >1){
				String startTime = splitTime[0];
				String endTime = splitTime[1];
				orderList = orderService.searchOrderByTime(startTime, endTime,isOrder);
			}
		}
		if(orderList != null && orderList.size() > 0){
			ActionContext.getContext().put("timeOrderList", orderList);
			ActionContext.getContext().put("timestr", time);
			Total total = new Total(orderList);
			ActionContext.getContext().put("total", total);
			ActionContext.getContext().put("isOrder", isOrder);
		}else{
			String msg = "对不起，无该段时间的清单，请核对后重新输入！";
			ActionContext.getContext().put("msg", msg);
		}
		ActionContext.getContext().put("radio", 1);
		return "searchByTime";
	}
	
	public String searchByName() throws Exception{
		List<Order> orderList = null;
		if(name != null){
			if(name.equals(new String(name.getBytes("iso8859-1"), "iso8859-1"))){
				name = new  String(name.getBytes( "iso8859-1"), "UTF-8");
			}
			
			//System.out.println(name);
			orderList = orderService.searchOrderByName(name,isOrder);
			if(orderList != null && orderList.size() > 0){
				ActionContext.getContext().put("nameOrderList", orderList);
				ActionContext.getContext().put("name", name);
				Total total = new Total(orderList);
				ActionContext.getContext().put("total", total);
				ActionContext.getContext().put("isOrder", isOrder);
			}else{
				String msg = "对不起，无该客户的清单，请检查客户名是否输错！";
				ActionContext.getContext().put("msg", msg);
			}
			ActionContext.getContext().put("radio", 2);
		}
		return "searchByName";
	}
	
	public String delete() throws Exception{
		String returnStr = null;
		if(id != null){
			orderService.deleteOrderByid(id);
			ActionContext.getContext().put("isOrder", isOrder);
			if(radio == 1){
				returnStr = "timedelete";
			}else if (radio == 2) {
				if(name!=null){
					if(name.equals(new String(name.getBytes("iso8859-1"), "iso8859-1"))){
						name = new  String(name.getBytes( "iso8859-1"), "UTF-8");
					}
				}
				ActionContext.getContext().put("name", name);
				returnStr = "namedelete";
			}else {
				returnStr="delete";
			}
		}
		
		return returnStr;
	}
	
	public String updatePay() throws Exception{
		orderService.updateRealPayByOrderid(id, realPay);
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("orderid", id);
		ActionContext.getContext().put("isOrder", isOrder);
		return "updatePay";
	}
	
	
	public String updateUI() throws Exception{
		if(id!=null ){
			Order order = orderService.searchOrderByOrderId(id,isOrder);
			if(order != null){
				ActionContext.getContext().put("order", order);
				ActionContext.getContext().put("radio", radio);
				ActionContext.getContext().put("isOrder", isOrder);
			}
			if(radio == 1){
				ActionContext.getContext().put("time", time);
			}
		}
		return "updateUI";
	}
	
	public String updateShow() throws Exception{
		int totalPay = 0;
		int realPay = 0;
		double totalNumber = 0;
		double totalMaleNum = 0;
		double totalFemaleNum = 0; 
		List<Crab> crabList = new ArrayList<Crab>();
		if(malecrabids !=null && malecrabids.length>0){
			for(int i = 0; i<malecrabids.length;i++){
				if(malemounts[i] != 0.0){
					Crab crab = crabService.findCrabById(malecrabids[i]);
					CrabPrice crabPrice = new CrabPrice();
					crabPrice.setPrice(malecrabPrice[i]);
					crab.setCrabPrice(crabPrice);
					crab.setNote(maledescribes[i]);
					crab.setNumber(malemounts[i]);
					crabList.add(crab);
					totalMaleNum += crab.getNumber();
					totalNumber += crab.getNumber();
					totalPay += crab.getTotalPrice();
				}
			}
		}
		
		if(femalecrabids != null && femalecrabids.length > 0){
			for(int i = 0; i<femalecrabids.length;i++){
				if(femalemounts[i] != 0.0){
					Crab crab = crabService.findCrabById(femalecrabids[i]);
					CrabPrice crabPrice = new CrabPrice();
					crabPrice.setPrice(femalecrabPrice[i]);
					crab.setCrabPrice(crabPrice);
					crab.setNote(femaledescribes[i]);
					crab.setNumber(femalemounts[i]);
					crabList.add(crab);
					totalFemaleNum += crab.getNumber();
					totalNumber += crab.getNumber();
					totalPay += crab.getTotalPrice();
				}
			}
		}
		realPay = totalPay;
		Order order = orderService.searchOrderByOrderId(id,isOrder);
		if(order != null){
			order.setCrabList(crabList);
			order.setClientName(name);
			order.setRealPay(realPay);
			order.setTotalFemaleNum(totalFemaleNum);
			order.setTotalMaleNum(totalMaleNum);
			order.setTotalNumber(totalNumber);
			order.setTotalPay(totalPay);
			ActionContext.getContext().put("order", order);
			ActionContext.getContext().getSession().put("order", order);
			ActionContext.getContext().put("radio", radio);
			ActionContext.getContext().put("isOrder", isOrder);
		}
		
		if(radio == 1){
			ActionContext.getContext().put("time", time);
		}
		
		return "updateShow";
	}
	
	public String update() throws Exception{
		String returnStr = null;
		Order order = (Order) ActionContext.getContext().getSession().get("order");
		order.setRealPay(realPay);
		orderService.updateOrder(order);
		ActionContext.getContext().getSession().remove("order");
		if(radio == 1){
			returnStr = "timeupdate";
		}else if (radio == 2) {
			returnStr = "nameupdate";
		}else {
			returnStr="update";
		}
		return returnStr;
	}
	
	public String detail() throws Exception{
		if(id!=null ){
			Order order = orderService.searchOrderByOrderId(id,isOrder);
			if(order != null){
				ActionContext.getContext().put("order", order);
				ActionContext.getContext().put("radio", radio);
				ActionContext.getContext().put("isOrder", isOrder);
			}else{
				String msg = "对不起，无该订单的信息，请核对后重新输入！";
				ActionContext.getContext().put("msg", msg);
			}
			
			if(radio == 1){
				ActionContext.getContext().put("time", time);
			}
		}
		return "detail";
	}
	
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public int[] getMalecrabids() {
		return malecrabids;
	}
	public void setMalecrabids(int[] malecrabids) {
		this.malecrabids = malecrabids;
	}
	public int[] getFemalecrabids() {
		return femalecrabids;
	}
	public void setFemalecrabids(int[] femalecrabids) {
		this.femalecrabids = femalecrabids;
	}
	public double[] getMalemounts() {
		return malemounts;
	}
	public void setMalemounts(double[] malemounts) {
		this.malemounts = malemounts;
	}
	public double[] getFemalemounts() {
		return femalemounts;
	}
	public void setFemalemounts(double[] femalemounts) {
		this.femalemounts = femalemounts;
	}
	
	public double[] getMalecrabPrice() {
		return malecrabPrice;
	}

	public void setMalecrabPrice(double[] malecrabPrice) {
		this.malecrabPrice = malecrabPrice;
	}

	public double[] getFemalecrabPrice() {
		return femalecrabPrice;
	}

	public void setFemalecrabPrice(double[] femalecrabPrice) {
		this.femalecrabPrice = femalecrabPrice;
	}

	public String[] getMaledescribes() {
		return maledescribes;
	}
	public void setMaledescribes(String[] maledescribes) {
		this.maledescribes = maledescribes;
	}
	public String[] getFemaledescribes() {
		return femaledescribes;
	}
	public void setFemaledescribes(String[] femaledescribes) {
		this.femaledescribes = femaledescribes;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getRadio() {
		return radio;
	}

	public void setRadio(int radio) {
		this.radio = radio;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getRealPay() {
		return realPay;
	}

	public void setRealPay(int realPay) {
		this.realPay = realPay;
	}

	public int getIsOrder() {
		return isOrder;
	}

	public void setIsOrder(int isOrder) {
		this.isOrder = isOrder;
	}

}
